package org.dashboard.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdDataset;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdDatasetMapper;
import org.dashboard.service.AdDatasetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = AdDatasetService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER, cacheNames = "dataset")
public class AdDatasetServiceImpl implements AdDatasetService {
    private static final Logger logger = LoggerFactory.getLogger(AdDatasetServiceImpl.class);

    @Autowired
    private AdDatasetMapper adDatasetMapper;

    @Override
    @CacheEvict(allEntries = true)
    public int deleteByPrimaryKey(Integer datasetId) {
        return adDatasetMapper.deleteByPrimaryKey(datasetId);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int insert(AdDataset record) {
        adDatasetMapper.insert(record);
        return record.getDatasetId();
    }

    @Override
    @CacheEvict(allEntries = true)
    public int insertSelective(AdDataset record) {
        adDatasetMapper.insertSelective(record);
        return record.getDatasetId();
    }

    @Override
    @Cacheable(key = "'datasetId:' + #datasetId.toString()")
    public AdDataset selectByPrimaryKey(Integer datasetId) {
        return adDatasetMapper.selectByPrimaryKey(datasetId);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKeySelective(AdDataset record) {
        return adDatasetMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKeyWithBLOBs(AdDataset record) {
        return adDatasetMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKey(AdDataset record) {
        return adDatasetMapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public boolean deleteByPrimaryKeys(ArrayList<Integer> datasetIds) {
        for (Integer id : datasetIds) {
            adDatasetMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

    @Override
    @Cacheable(key = "'datasetCode:' + #datasetCode.toString()")
    public AdDataset selectAdDatasetByCode(String datasetCode) {
        return adDatasetMapper.selectAdDatasetByCode(datasetCode);
    }

    @Override
    public List<AdDataset> selectAdDatasets() {
        return adDatasetMapper.selectAdDatasets();
    }

    @Override
    public int checkDatasetCodeExist(String datasetCode, Integer datasetId) {
        return adDatasetMapper.checkDatasetCodeExist(datasetCode, datasetId);
    }


    @Override
    public List<AdDataset> selectAdDatasetsBySchemaId(Integer schemaId) {
        return adDatasetMapper.selectAdDatasetsBySchemaId(schemaId);
    }

    @Override
    public PageInfo selectAdDatasetsPaged(String criteria, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdDataset> adDatasets = adDatasetMapper.selectAdDatasetsSearched(criteria);
        return new PageInfo<>(adDatasets);
    }
}
